<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;
use App\Helper\Calendar;
use App\Service\UserService;
use App\Service\WxTemplateMessageService;
use Hyperf\Utils\ApplicationContext;

/**
 * 系统公共服务
 */
class CommonService 
{
    /**
     * [__construct description] 构造函数
     */
    public function __construct()
    {
    }
    /**
     * 设备号保存
     * [regIdSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function regIdSave($params)
    {
        $exists_id = Db::table('user_push_reg')
            ->where(['user_id'=>$params['user_id']])
            ->value("id");
        if(!empty($exists_id)){
            $save_data = [
                'updated_at'=>time()
            ];
            if(!empty($params['platform'])){
                $save_data['platform'] = $params['platform'];
            }
            if(!empty($params['device'])){
                $save_data['device'] = $params['device'];
            }
            if(!empty($params['reg_id'])){
                $save_data['reg_id'] = $params['reg_id'];
            }
            Db::table('user_push_reg')
                ->where(['id'=>$exists_id])
                ->update($save_data);
        }else{
            $save_data = [
                'user_id'=>$params['user_id'],
                'platform'=>$params['platform'],
                'device'=>$params['device'],
                'reg_id'=>$params['reg_id'],
                'created_at'=>time()
            ];
            Db::table('user_push_reg')
                ->insert($save_data);
        }
        $this->regIdRedisSave($params);
        return true;
    }
    /**
     * 设备号保存到redis
     * [regIdRedisSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function regIdRedisSave($params)
    {
        $container = ApplicationContext::getContainer();
        $redis = $container->get(\Redis::class);
        $redis_key = "grabpiao_pusher:regids";
        $redis_data = json_encode($params, JSON_UNESCAPED_UNICODE);
        $redis->HSET($redis_key, (string)$params['user_id'], $redis_data);
        return true;
    }
    /**
     * 悬浮秒表信息
     * [watchInfo description]
     * @return [type] [description]
     */
    public function watchInfo()
    {
        $default = [
            [
                'id'=>'local',
                'name'=>'本机时间',
                'image'=>env("QINIU_URL").'grabpiao/app/icon_times_iph.png',
                'watch_url' => ''
            ],
            [
                'id'=>'bj',
                'name'=>'北京时间',
                'image'=>env("QINIU_URL").'grabpiao/app/icon_times_bj.png',
                'watch_url' => 'https://www.tsa.cn/api/time/getCurrentTime'
            ]
        ];
        $platform = $data = Db::table('platform')
            ->select(['id','name','image','watch_url'])
            ->where(['status'=>1,'is_del'=>0])
            ->orderBy("sort","desc")
            ->get()
            ->toArray();
        $return_data = array_merge($default,$platform);
        return $return_data;
    }
}
